# Import the base Model class from Flask-AppBuilder.
from flask_appbuilder import Model
# Import various column types from SQLAlchemy to define the database schema.
from sqlalchemy import BigInteger, Column, Date, Float, Integer, String, Text

# Import a custom base model class from the application.
from myapp.models.base import MyappModelBase

# Get the metadata object from the base Flask-AppBuilder Model.
# This metadata is shared across all models and holds the schema information.
metadata = Model.metadata


# Define the Metadata_table class, which maps to the 'metadata_table' in the database.
# This model is intended to store metadata about database tables.
class Metadata_table(Model, MyappModelBase):
    # Specify the table name in the database.
    __tablename__ = 'metadata_table'
    # Define the primary key column.
    id = Column(Integer, primary_key=True)
    # A unique identifier for the node or source system.
    node_id = Column(String(200))
    # The application or service that owns this table.
    app = Column(String(200), nullable=True)
    # The full name of the organization.
    c_org_fullname = Column(String(255), nullable=True, default='')
    # The name of the database.
    db = Column(String(200), nullable=True)  # 库名
    # The name of the table.
    table = Column(String(400), nullable=True)
    # A JSON string representing metadata about the table's columns.
    metadata_column = Column(Text, nullable=True, default='[]')
    # A description of the table.
    describe = Column(String(200), nullable=True)
    # The owner of the table.
    owner = Column(String(200), nullable=True)
    # The lifecycle or retention period of the data in days.
    lifecycle = Column(Integer, nullable=True, default=0)
    # A recommended lifecycle for the data.
    rec_lifecycle = Column(Integer, nullable=True, default=0)
    # The storage size of the table (e.g., "10.5 GB").
    storage_size = Column(String(200), nullable=True, default='0')
    # The estimated cost of storing the data.
    storage_cost = Column(Float, nullable=True, default=0)
    # The number of visits or queries in the last 7 days.
    visits_seven = Column(Integer, nullable=True, default=0)
    # The number of visits in the last 30 days.
    visits_thirty = Column(BigInteger, nullable=True, default=0)
    # The number of visits in the last 60 days.
    visits_sixty = Column(BigInteger, nullable=True, default=0)
    # The date of the most recent visit.
    recent_visit = Column(Date, nullable=True)
    # The start partition value (for partitioned tables).
    partition_start = Column(String(255), nullable=True, default='')
    # The end partition value.
    partition_end = Column(String(255), nullable=True, default='')
    # The status of the metadata record (e.g., active, deprecated).
    status = Column(String(255), nullable=True, default='')
    # The user who created the metadata record.
    creator = Column(String(255), nullable=True, default='')

    # The DDL (Data Definition Language) statement used to create the table.
    create_table_ddl = Column(Text, nullable=True)
    # A text field for additional column information.
    col_info = Column(Text, nullable=True)
    # The mode for updating partitions, as an integer enum.
    partition_update_mode = Column(Integer, nullable=True)
    # A flag indicating if the user has privileges on this table.
    is_privilege = Column(Integer, nullable=False, default=1)
    # The source of the data, as an integer enum.
    data_source = Column(Integer, nullable=True, default=0)

    # The data domain to which this table belongs.
    field = Column(String(200), nullable=True, default='未知')  # 所属数据域
    # The security or sensitivity level of the data.
    security_level = Column(String(200), nullable=True, default='普通')  # 安全等级
    # A score representing the value or importance of the data.
    value_score = Column(String(200), nullable=True, default='0')  # 价值评估
    # The level of the table within the data warehouse architecture (e.g., ODS, DWD, DWS).
    warehouse_level = Column(String(200), nullable=True, default='未知')  # 数仓级别
    # The Time-To-Live or data retention period.
    ttl = Column(String(200), nullable=True)  # 保留时长

    # A JSON string for storing extensible, miscellaneous metadata.
    expand = Column(Text(65536), nullable=True, default='{}')

    # Define the string representation for an instance of this model.
    def __repr__(self):
        # When an object is printed, it will show the table name.
        return self.table
